﻿using Prism.Regions;
using Prism.Services.Dialogs;
using SmartPlant.Model.Biz;
using SmartPlant.Model.DbContext;
using SmartPlant.Services;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Data;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;
using Unity;

namespace SmartPlant.Core.Mvvm
{
    /// <summary>
    /// 支持动态列加载
    /// </summary>
    public abstract class DynamicColumnRegionViewModel : RegionViewModelBase
    {
        private RadObservableCollection<GridViewColumn> _columns;
        private RadObservableCollection<GridViewColumnGroup> _columnGroups;
        private RadObservableCollection<GroupDescriptor> _groupDescriptors;
        private ICodeListService _codeListService;
        /// <summary>
        /// 动态列信息。比如初始化IndexTable时，就用<see cref="GlobalObject.IndexTablePropertyInfos"/>里拿
        /// </summary>
        protected List<DynamicPropertyInfo> _dynamicPropertyInfos;

        protected DynamicColumnRegionViewModel(
            IRegionManager regionManager,
            IUnityContainer unityContainer,
            IDialogService dialogService)
            : base(regionManager, unityContainer, dialogService)
        {
            _codeListService = unityContainer.Resolve<CodeListService>();
            _dynamicPropertyInfos = new List<DynamicPropertyInfo>();
            LoadPropertyInfos();
        }

        protected virtual void LoadPropertyInfos()
        {

        }

        /// <summary>
        /// 获取或设置分组描述列。
        /// </summary>
        public RadObservableCollection<GroupDescriptor> GroupDescriptors
        {
            get
            {
                if (_groupDescriptors != null)
                    return _groupDescriptors;
                _groupDescriptors = GetGroupDescriptors(_dynamicPropertyInfos);
                return _groupDescriptors;
            }
            set
            {
                SetProperty(ref _groupDescriptors, value);
            }
        }

        /// <summary>
        /// 获取或设置分组描述列。
        /// </summary>
        public RadObservableCollection<GridViewColumnGroup> ColumnGroups
        {
            get
            {
                if (_columnGroups != null)
                    return _columnGroups;
                _columnGroups = GetGroupColumns(_dynamicPropertyInfos);
                return _columnGroups;
            }
            set
            {
                SetProperty(ref _columnGroups, value);
            }
        }

        /// <summary>
        /// 获取动态表格列。(包括可见的和不可见的）
        /// </summary>
        public RadObservableCollection<GridViewColumn> Columns
        {
            get
            {
                if (_columns != null)
                    return _columns;
                _columns = GetGridColumns(_dynamicPropertyInfos);
                return _columns;
            }
            set
            {
                SetProperty(ref _columns, value);
            }
        }

        /// <summary>
        /// 根据动态属性集合获取数据视图列。
        /// </summary>
        /// <returns></returns>
        protected RadObservableCollection<GridViewColumn> GetGridColumns(IList<DynamicPropertyInfo> list)
        {
            var columns = new RadObservableCollection<GridViewColumn>();
            columns.SuspendNotifications();
            

            foreach (var item in list)
            {
                List<DropDownListObj> dropDownList = new List<DropDownListObj>();
                //if (!item.IsDisplayed) continue;
                var task = Task.Run(async () =>
               {
                   dropDownList = await _codeListService.GetDropDownListByAttrAsync(item.ItemAttributionId);//  GlobalObject.GetDropDownListByAttr(item.ItemAttributionId);
               });
                task.Wait();
                GridViewColumn col = new GridViewDataColumn()
                {
                    UniqueName = item.PropertyName,
                    DataMemberBinding = new Binding(item.PropertyName),
                    Header = item.DisplayName,
                    Width = new GridViewLength(item.Width.ToDouble()),
                    IsFilterable = true,
                    IsSortable = true,
                    //IsVisible = !item.IsGroup,
                    IsVisible = item.IsVisible,
                    IsReadOnly = item.IsReadOnly,
                    ColumnGroupName = item.PropertyName,
                    DisplayIndex = (int)item.Order,
                    ToolTip = item.SPITableName + "." + item.SPIColName,
                };

                if (dropDownList != null && dropDownList.Count > 0)
                {
                    col.Tag = new SimpleModel<string, decimal>//Tag当做其下拉列表的属性来用
                    {
                        v1 = item.PropertyName,//动态类属性名
                        v2 = item.ItemAttributionId//动态类下拉列表ID。
                    };
                }
                columns.Add(col);

            }
            columns.ResumeNotifications();
            return columns;
        }


        /// <summary>
        /// 根据动态属性集合获取分组列。
        /// </summary>
        /// <returns></returns>
        protected RadObservableCollection<GridViewColumnGroup> GetGroupColumns(IList<DynamicPropertyInfo> list)
        {
            var group = from v in list
                        where v.IsGroup
                        select v;
            if (group != null && group.Count() > 0)
            {
                var groupCols = new RadObservableCollection<GridViewColumnGroup>();
                groupCols.SuspendNotifications();
                foreach (var item in group)
                {
                    var gridViewColumn = new GridViewColumnGroup
                    {
                        Name = item.PropertyName,
                        Header = item.DisplayName,
                    };
                    groupCols.Add(gridViewColumn);
                }
                groupCols.ResumeNotifications();
                return groupCols;
            }
            return null;
        }

        /// <summary>
        /// 根据动态属性集合获取分组描述列。
        /// </summary>
        /// <returns></returns>
        protected RadObservableCollection<GroupDescriptor> GetGroupDescriptors(IList<DynamicPropertyInfo> list)
        {
            var group = from v in list
                        where v.IsGroup
                        select v;
            if (group != null && group.Count() > 0)
            {
                var groupDescriptors = new RadObservableCollection<GroupDescriptor>();
                groupDescriptors.SuspendNotifications();
                foreach (var item in group)
                {
                    var gridViewColumn = new GroupDescriptor
                    {
                        Member = item.PropertyName,
                        DisplayContent = item.DisplayName,
                    };
                    groupDescriptors.Add(gridViewColumn);
                }
                groupDescriptors.ResumeNotifications();
                return groupDescriptors;
            }
            return null;
        }
    }
}


//if (dropDownList != null && dropDownList.Count > 0 && dropDownList.Count < 1000)
//{
//    col = new GridViewComboBoxColumn()
//    {
//        DataMemberBinding = new Binding(item.PropertyName),
//        Header = item.DisplayName,
//        Width = new GridViewLength(item.Width.ToDouble()),
//        SelectedValueMemberPath = "Name",
//        DisplayMemberPath = "Name",
//        ItemsSource = dropDownList
//    };
//}